package AionCrafter.data;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;

import AionCrafter.data.list.MaterialList;
import AionCrafter.data.list.RecipeList;

public class CSVParser {
	
	/**
	 * Access the Items CSV file
	 * @return
	 * A Material List (Essentially a Map<itemId,ItemName>)
	 */
	public MaterialList loadMaterialList(){
		MaterialList mList = new MaterialList();
		
		Scanner s = null;
		try{
			s = new Scanner(new BufferedReader(new FileReader("./data/items.csv")));
			s.useDelimiter("\n");
			
			while (s.hasNext()){
				String line = s.next();
				StringTokenizer st = new StringTokenizer(line, ",\r");
				String name = st.nextToken();
				String id = st.nextToken();
				int price = Integer.parseInt(st.nextToken());
				mList.addMaterial(id, new Material(name, price));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (s != null) 
				s.close();
		}
		
		return mList;
	}
	
	/**
	 * Access the Recipe CSV files
	 * @param mode
	 * Possible modes{alchemy, armor, cooking, handicraft, tailor, weapon}
	 * @return
	 * Recipe List for the given mode
	 */
	public RecipeList loadRecipeList(String mode){
		RecipeList rList = new RecipeList();
		String path = "./data/"+mode+".csv";
		
		Scanner s = null;
		try{
			s = new Scanner(new BufferedReader(new FileReader(path)));
			s.useDelimiter("\n");
			
			while(s.hasNext()){
				String line = s.next();
				StringTokenizer st = new StringTokenizer(line, ",");
				String name = cleanUp(st.nextToken(), "\"");
				String id = cleanUp(st.nextToken(), "\"");
				int quantityProduced = Integer.parseInt(st.nextToken());
				int craftLevel = Integer.parseInt(st.nextToken());
				String profession = cleanUp(st.nextToken(), "\"");
				String race = (cleanUp(st.nextToken(), "\"").equals("pc_light") ? "Elyos" : "Asmodian");
				
				ArrayList<String> compStrings = new ArrayList<String>();
				for(int i = 0; i < 7; i++){
					String component = cleanUp(st.nextToken(), "\"");
					if(!component.equals("\\N")) compStrings.add(component);
				}
				
				ArrayList<Integer> compQuantities = new ArrayList<Integer>();
				for(int i = 0; i < 7; i++){
					String quantity = cleanUp(st.nextToken(), "\"");
					if(!quantity.equals("\\N")) compQuantities.add(Integer.parseInt(quantity));
				}
				
				Map<String,Integer> components = new HashMap<String,Integer>();
				for(int i = 0; i < compStrings.size(); i++){
					components.put(compStrings.get(i), compQuantities.get(i));
				}
				//temporary to stop duplicate recs between races. just showing elyos :-)
				if(race.equals("Elyos")){
					rList.addRecipe(id, new Recipe(name, quantityProduced, craftLevel, profession, race, components));
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (s != null) 
				s.close();
		}
		
		return rList;
	}
	
	/**
	 * Cleans a string by removing certain bad characters wherever they're placed
	 * @param s
	 * @param badChars
	 * @return
	 */
	private String cleanUp(String s, String badChars){
		int size = s.length();
		StringBuffer buf = new StringBuffer(size);
		for(int i = 0; i < size; i++){
			if(badChars.indexOf(s.charAt(i)) == -1) buf.append(s.charAt(i));
		}
		return buf.toString();
	}

}
